package com.cacshanghai.cac_oa_client.ui;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;

import com.cacshanghai.cac_oa_client.R;
import com.cacshanghai.cac_oa_client.beans.Leave;

import java.util.ArrayList;
import java.util.List;

import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;

/**
 * Created by geyh on 2016/5/30.
 */
public class LeaveApprovalActivity extends BaseActivity implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {

    private PtrClassicFrameLayout mPtrFrame;
    private ListView listView;
    private List<Leave> leaveList = new ArrayList<>();
    private LeaveApprovalAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_leave_approval);
        setTitle(getString(R.string.leave_approval) + "(葛仪豪)");
        setBack();

        setData();

        listView = (ListView) findViewById(R.id.rotate_header_list_view);
        listView.setOnItemClickListener(this);
        listView.setOnItemLongClickListener(this);

        adapter = new LeaveApprovalAdapter();

        mPtrFrame = (PtrClassicFrameLayout) findViewById(R.id.rotate_header_list_view_frame);
        mPtrFrame.setLastUpdateTimeRelateObject(this);
        mPtrFrame.setPtrHandler(new PtrHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                frame.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        listView.setAdapter(adapter);
                        mPtrFrame.refreshComplete();
                    }
                }, 1800);
            }

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
            }
        });
        mPtrFrame.postDelayed(new Runnable() {
            @Override
            public void run() {
                mPtrFrame.autoRefresh();
            }
        }, 100);
    }

    private void setData() {
        leaveList.add(new Leave());
        leaveList.add(new Leave());
        leaveList.add(new Leave());
        leaveList.add(new Leave());
        leaveList.add(new Leave());
        leaveList.add(new Leave());
        leaveList.add(new Leave());
        leaveList.add(new Leave());
        leaveList.add(new Leave());
        leaveList.add(new Leave());
        leaveList.add(new Leave());
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Leave leave = leaveList.get(position);
        leave.setChecked(!leave.isChecked());

        ImageView iv_check = (ImageView) view.findViewById(R.id.iv_check);
        if (leave.isChecked()) {
            iv_check.setImageResource(R.mipmap.check);
        } else {
            iv_check.setImageResource(R.mipmap.check_circle);
        }
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        AlertDialog.Builder builder = new AlertDialog.Builder(LeaveApprovalActivity.this);
        builder.setItems(R.array.approval, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        }).create().show();
        return true;
    }


    class LeaveApprovalAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return leaveList.size();
        }

        @Override
        public Leave getItem(int position) {
            return leaveList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Leave leave = getItem(position);
            if (convertView == null) {
                convertView = LayoutInflater.from(LeaveApprovalActivity.this).inflate(R.layout.item_leave_approval, null);
            }

            ImageView iv_check = (ImageView) convertView.findViewById(R.id.iv_check);
            if (leave.isChecked()) {
                iv_check.setImageResource(R.mipmap.check);
            } else {
                iv_check.setImageResource(R.mipmap.check_circle);
            }


            return convertView;
        }
    }
}
